package tq.com.tqstudent.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lib.common.base.BaseActivity;
import com.lib.common.utils.AppManager;
import com.lzy.okgo.OkGo;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.SimpleBaseBean;
import tq.com.tqstudent.bean.params.LoginParams;
import tq.com.tqstudent.bean.params.ModifyPwdParams;
import tq.com.tqstudent.net.ok.SimpleDialogCallback;
import tq.com.tqstudent.widgets.CountDownTextView;

/**
 * 注册和找回密码
 * @Author rames
 * @date 2019/2/27 17:35
 */
public class RegisterActivity extends BaseActivity {
    @BindView(R.id.phone_edit)
    EditText phoneEdit;
    @BindView(R.id.verify_pwd_edit)
    EditText verifyPwdEdit;
    @BindView(R.id.pwd_edit)
    EditText pwdEdit;
    @BindView(R.id.verify_tv)
    CountDownTextView verifyTV;
    @BindView(R.id.commit_tv)
    TextView commitTV;
    private PWD_TYPE pwdTYpe = PWD_TYPE.REGISTER;

    public static void show(Activity activity, PWD_TYPE type){
        Intent intent = new Intent(activity, RegisterActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("pwd_type", type);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public enum PWD_TYPE{
        REGISTER, FIND_PWD, MODIFY_PWD
    }

    @Override
    protected void getExtras(Bundle extras) {
        pwdTYpe = (PWD_TYPE) extras.getSerializable("pwd_type");
    }

    @Override
    public int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        if(pwdTYpe == PWD_TYPE.REGISTER){
            setTitle("注册");
            commitTV.setText("完成");
            pwdEdit.setHint("请设置登录密码");

        } else if(pwdTYpe == PWD_TYPE.FIND_PWD){
            setTitle("重置密码");
            commitTV.setText("确定");
            pwdEdit.setHint("请设置新密码");

        } else {
            setTitle("修改密码");
            commitTV.setText("确定");
            pwdEdit.setHint("请设置新密码");
        }

        countDownTime();
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.commit_tv})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.commit_tv:
                // 注册
                if(pwdTYpe == PWD_TYPE.REGISTER){
                    if(check()) {
                        registerTask();
                    }

                    // 找回密码
                } else if(pwdTYpe == PWD_TYPE.FIND_PWD){
                    if(check()){
                        findPwdTask();
                    }

                    // 修改密码
                } else {
                    if(check()){
                        modifyPwdTask();
                    }
                }
                break;
        }
    }

    private void countDownTime(){
        verifyTV.setNormalText("获取验证码")
                .setCountDownText("重新获取(", ")")
                .setCloseKeepCountDown(false)//关闭页面保持倒计时开关
                .setCountDownClickable(false)//倒计时期间点击事件是否生效开关
                .setShowFormatTime(true)//是否格式化时间
                .setOnCountDownFinishListener(new CountDownTextView.OnCountDownFinishListener() {
                    @Override
                    public void onFinish() {
                    }
                })
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(phoneEdit.getText().toString().trim().length() != 11){
                            showToast("请输入正确的手机号");
                        } else {
                            if(pwdTYpe == PWD_TYPE.REGISTER){
                                getVerifyTask(true);

                            } else {
                                getVerifyTask(false);
                            }

                            verifyTV.startCountDown(Constants.COUNTDOWN_TIME);
                        }
                    }
                });
    }

    /**
     * 获取验证码
     * @param isRegister 是否是获取注册的验证码
     */
    private void getVerifyTask(boolean isRegister){
        String json = "{\"phone\":\"" + phoneEdit.getText().toString().trim() + "\"}";;
        OkGo.<SimpleBaseBean>post(App.getHttpUrl(isRegister ? "api/user/signupsms" : "api/user/authsms"))
                .tag(this)
                .upJson(json)
                .execute(new SimpleDialogCallback(RegisterActivity.this) {
                    @Override
                    public void handleSuccess(SimpleBaseBean data) {
                        if(data.isSuccess()){
                            showToast("验证码已发送");
                        } else {
                            showToast(data.getMsg());
                        }
                    }
                });
    }

    /** 注册 */
    private void registerTask(){
        LoginParams params = new LoginParams();
        params.name = phoneEdit.getText().toString().trim();
        params.pwd = pwdEdit.getText().toString().trim();
        params.phone = phoneEdit.getText().toString().trim();
        params.sms = verifyPwdEdit.getText().toString().trim();
        String parStr = new Gson().toJson(params);

        OkGo.<SimpleBaseBean>post(App.getHttpUrl("api/user/signup"))
                .tag(this)
                .upJson(parStr)
                .execute(new SimpleDialogCallback(RegisterActivity.this) {
                    @Override
                    public void handleSuccess(SimpleBaseBean data) {
                        if(data.isSuccess()){
                            showToast("注册成功");
                            AppManager.getAppManager().popToActivity(LoginActivity.class);
                        } else {
                            showToast(data.getMsg());
                        }
                    }
                });
    }

    /** 找回密码 */
    private void findPwdTask(){
        ModifyPwdParams params = new ModifyPwdParams();
        params.name = phoneEdit.getText().toString().trim();
        params.new_pwd = pwdEdit.getText().toString().trim();
        params.smscode = verifyPwdEdit.getText().toString().trim();
        String parStr = new Gson().toJson(params);

        OkGo.<SimpleBaseBean>post(App.getHttpUrl("api/user/chgpwd"))
                .tag(this)
                .upJson(parStr)
                .execute(new SimpleDialogCallback(RegisterActivity.this) {
                    @Override
                    public void handleSuccess(SimpleBaseBean data) {
                        if(data.isSuccess()){
                            showToast("密码修改成功");
                            AppManager.getAppManager().popToActivity(LoginActivity.class);
                        } else {
                            showToast(data.getMsg());
                        }
                    }
                });
    }

    /** 修改密码 */
    private void modifyPwdTask(){
        ModifyPwdParams params = new ModifyPwdParams();
        params.name = phoneEdit.getText().toString().trim();
        params.new_pwd = pwdEdit.getText().toString().trim();
        params.smscode = verifyPwdEdit.getText().toString().trim();
        String parStr = new Gson().toJson(params);

        OkGo.<SimpleBaseBean>post(App.getHttpUrl("api/user/chgpwd"))
                .tag(this)
                .upJson(parStr)
                .execute(new SimpleDialogCallback(RegisterActivity.this) {
                    @Override
                    public void handleSuccess(SimpleBaseBean data) {
                        if(data.isSuccess()){
                            showToast("密码修改成功");
                            RegisterActivity.this.finish();
//                            AppManager.getAppManager().popToActivity(LoginActivity.class);
                        } else {
                            showToast(data.getMsg());
                        }
                    }
                });
    }

    private boolean check(){
        String phone = phoneEdit.getText().toString().trim();
        if(phone.length() != 11){
            showToast("请输入正确的手机号");
            return false;
        }

        String sms = verifyPwdEdit.getText().toString().trim();
        if(sms.length() != 6){
            showToast("请输入正确的6位数字验证码");
            return false;
        }

        String pwd = pwdEdit.getText().toString().trim();
        if(TextUtils.isEmpty(pwd) && pwd.length() < 6){
            showToast("密码不能为空且不小于6位");
            return false;
        }

        return true;
    }
}
